<? 
	include('base.php');
	User::protect();
	include_class('shows');
	include_class('m2');
	$section = 'shows';
	$type = ($_GET['type'] == 'av') ? 'av' : 'photos';
	$sh = Show::get($_GET['id']);
	
	if (!db::isError($sh)) {
		$ma = ($type == 'av') ? $sh->getAVAreaObject() : $sh->getPhotoAreaObject();	
	
		switch($_GET['task']) {
			case 'register_media':
				$res = $sh->registerMedia($_GET['type']);				
				if (!db::isError($res)) {
					header('Location: show_media.php?id=' . $_GET['id'] . '&type=' . $type);
				}
				break;
			case 'rescan':
				if (!db::isError($ma)) {
					$res = $ma->rescan($sh);
					if (!db::isError($res)) {
						header('Location: show_media.php?id=' . $_GET['id']);
					}
				}
				break;
			case 'move_up':
				if (!db::isError($ma)) {
					$mi = MediaInstance::get($_GET['media_instance_id']);
					if (!db::isError($mi)) {
						$type = ($mi->getAreaID() == $sh->getAVAreaID()) ? "av" : "photos";
						$res = $mi->moveUp($sh);
						if (!db::isError($res)) {
							header('Location: show_media.php?id=' . $_GET['id'] . '&type=' . $type);
						}
					} else {
						$res = $mi;
					}
				}
				break;
			case 'move_down':
				if (!db::isError($ma)) {
					$mi = MediaInstance::get($_GET['media_instance_id']);
					if (!db::isError($mi)) {
						$res = $mi->moveDown($sh);
						$type = ($mi->getAreaID() == $sh->getAVAreaID()) ? "av" : "photos";
						if (!db::isError($res)) {
							header('Location: show_media.php?id=' . $_GET['id'] . '&type=' . $type);
						}
					} else {
						$res = $mi;
					}
				}
				break;
		}
	}
	
	$page_title = 'Show Media';
	include('layout/header.php');

?>

<?	if (!db::isError($sh)) { ?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="live.php">Live &#62;</a>
	<a href="shows.php">Shows &#62;</a>
	<a href="show_edit.php?id=<?=$_GET['id']?>">View/Edit Show &#62;</a>
	Media &#62;
</div>



<?	
	if ($res) { // it's an error
		if (db::isError($res)) {
			$res->outputList();
		}
	}
	
	if (is_object($ma) && !db::isError($ma)) {
	
		$exitURL = "show_edit.php?id=" . $_GET['id'];
		$exitText = "Return to Show";
		$canEdit = User::isAdmin();
		if ($canEdit) {
			$addURL = "show_media_add.php?id=" . $_GET['id'] . '&type=' . $type;
		}
		$editURL = "show_media_edit.php?id={$_GET['id']}&amp;";
		if ($ma->getDisplay() == 'grid') {
			include('layout/media_grid.php'); 
		} else {
			include('layout/media_list.php');
		}
	} else if (User::isAdmin()) {
	
		$ma->outputDialog('Register Show', $PHP_SELF . '?task=register_media&amp;id=' . $_GET['id'] . '&amp;type=' . $type);
	
	} else {
	
		$ma->outputDialog('Return to Show','show_edit.php?id=' . $_GET['id']);
		
	 } ?>


<? } else { ?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="live.php">Live &#62;</a>
	<a href="shows.php">Shows &#62;</a>
	Invalid Entry &#62;
</div>

<?=$sh->outputDialog('Return to Shows','shows.php', 'Please specify a valid show ID.');?>

<? } ?>

<? include('layout/footer.php'); ?>